package com.yjs.ppjokedemo.ui.sofa;

import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.yjs.ppjokedemo.R;
import com.yjs.ppjokedemo.adapter.VideoListAdapter;
import com.yjs.ppjokedemo.bean.VideoProduct;
import com.yjs.ppjokedemo.databinding.FragmentVideoBinding;
import com.yjs.ppjokedemo.viewmodel.VideoViewModel;

import java.util.List;


public class VideoFragment extends Fragment implements VideoListAdapter.VideoInterface {

    FragmentVideoBinding mFragmentVideoBinding;
    private VideoViewModel mVideoViewModel;
    private VideoListAdapter mVideoListAdapter;
    private NavController navController;
    LinearLayoutManager mLinearLayoutManager;

    public VideoFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mFragmentVideoBinding = FragmentVideoBinding.inflate(inflater,container,false);
        return mFragmentVideoBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

//        mLinearLayoutManager = new LinearLayoutManager(getContext());
        mVideoListAdapter = new VideoListAdapter(this);
        mFragmentVideoBinding.rvHomeVideo.setAdapter(mVideoListAdapter);

        mVideoViewModel = new ViewModelProvider(requireActivity()).get(VideoViewModel.class);
        //通过ViewModel获取到Repo数据源的方法进行解析
        mVideoViewModel.getProducts().observe(getViewLifecycleOwner(), new Observer<List<VideoProduct>>() {
            @Override
            public void onChanged(List<VideoProduct> videoProducts) {
                //给Adapter赋值,传递List数据源
                mVideoListAdapter.submitList(videoProducts);
            }
        });

        //自动播放
        //获取到当前开发版本和安卓系统的版本号
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            //滑动方法
            mFragmentVideoBinding.rvHomeVideo.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                @Override
                public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    RecyclerView.LayoutManager layoutManager = mFragmentVideoBinding.rvHomeVideo.getLayoutManager();
                    //通过二元运算符 判断返回true或false
                    if (layoutManager instanceof LinearLayoutManager){
                        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
                        //下标
                        int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                        if (firstVisibleItemPosition>=0){
                            View position = linearLayoutManager.findViewByPosition(firstVisibleItemPosition);
                            StandardGSYVideoPlayer standardGSYVideoPlayer = (StandardGSYVideoPlayer) position.findViewById(R.id.gsy);
                            standardGSYVideoPlayer.startPlayLogic();
                        }
                    }
                }
            });
        }


    }
}